package com.ai.bitcoin.jpa.service.impl;

import com.ai.bitcoin.jpa.dao.CoinPriceEstimateDao;
import com.ai.bitcoin.jpa.entity.CoinPriceEstimate;
import com.ai.bitcoin.jpa.service.CoinPriceEstimateService;
import com.ai.bitcoin.redis.service.BccRedisService;
import com.ai.bitcoin.util.JacksonUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CoinPriceEstimateServiceImpl implements CoinPriceEstimateService {

    @Autowired
    private CoinPriceEstimateDao coinPriceEstimateDao;

    @Autowired
    private BccRedisService bccRedisService;

    @Override
    public String queryCoinTrend(String bccId) {
        String h24Price = bccRedisService.get24hPrice(bccId);
        List<CoinPriceEstimate> list = coinPriceEstimateDao.findDistinctByBccIdAndLatest(bccId, 1);
        String feature24Price = JacksonUtils.toJson(list);
        String trend = formatBeforeAndAfter24hPrice(h24Price, feature24Price);
        return trend;
    }

    @Override
    public String findTodayRecommend(String today) {
        List<Object> list = coinPriceEstimateDao.findTodayRecommend();
        return JacksonUtils.toJson(list);
    }


    /**
     * 币24小时价格明细 实际价格+预测价格
     *
     * @param h24Price      实时价格
     * @param future24Price 预测价格
     * @return
     */
    private String formatBeforeAndAfter24hPrice(String h24Price, String future24Price) {
        JSONArray jsonArray = JSONArray.fromObject(h24Price);
        JSONArray jsonArray2 = JSONArray.fromObject(future24Price);
        JSONArray result = new JSONArray();
        if (jsonArray.size() == 0 || jsonArray2.size() == 0) {
            return result.toString();
        }
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject node = jsonArray.getJSONObject(i);
            if (node == null || node.keySet().size() == 0) {
                continue;
            }
            String s = node.optString("priceTimestamp");
            if (StringUtils.isNotBlank(s)) {
                StringBuilder stringBuilder = new StringBuilder(s);
                stringBuilder.replace(s.indexOf(":") + 1, s.length(), "00:00");
                if (result.size() == 0) {
                    CoinPriceEstimate coinPriceEstimate = new CoinPriceEstimate();
                    coinPriceEstimate.setDs(stringBuilder.toString());
                    coinPriceEstimate.setYhat(node.optString("priceCny"));
                    result.add(coinPriceEstimate);
                    continue;

                }
                //没有重复的日期
                Boolean flag = true;
                for (int j = 0; j < result.size(); j++) {
                    if (result.getJSONObject(j) != null) {
                        if (result.getJSONObject(j).optString("ds").equals(stringBuilder.toString())) {
                            flag = false;
                        }

                    }
                }
                if (flag) {
                    CoinPriceEstimate coinPriceEstimate = new CoinPriceEstimate();
                    coinPriceEstimate.setDs(stringBuilder.toString());
                    coinPriceEstimate.setYhat(node.optString("priceCny"));
                    result.add(coinPriceEstimate);
                }
            }
        }

        //去重
        jsonArray2.removeAll(result);
        result.addAll(jsonArray2);


        JSONObject last = result.getJSONObject(result.size() - 1);
        String uppder_bound_mean = last.optString("uppderBoundMean");
        String lower_bound_mean = last.optString("lowerBoundMean");

        last.put("uppderBoundMean", uppder_bound_mean.substring(0, uppder_bound_mean.indexOf(".") + 3));
        last.put("lowerBoundMean", lower_bound_mean.substring(0, lower_bound_mean.indexOf(".") + 3));

        return result.toString();
    }

}
